package com.open.capacity.notice.dmo;

import java.util.Objects;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.open.capacity.notice.dto.SendMessageJobDto;
import com.open.capacity.notice.enums.RececiverType;
import com.open.capacity.notice.enums.SendMessageJobStatus;

import cn.hutool.core.codec.Base64;
import io.swagger.annotations.ApiModelProperty;
import io.vavr.control.Try;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * <p>
 * 发送任务
 * </p>
 * 
 * @since 2024-05-06
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@TableName("msg_send_job")
public class SendMessageJob extends AbstractSendEntity {

	private static final long serialVersionUID = 1088269216824416034L;

	@ApiModelProperty(value = "任务名称")
	private String jobName;

	@ApiModelProperty(value = "关联消息模板")
	private String templateId;

	@ApiModelProperty(value = "接收类型:消息的发送ID类型：1.接收组 2.userId 3.手机号 4.微信")
	private RececiverType receciverType;

	@ApiModelProperty(value = "消息解析内容")
	private String sendMessage ;
	
	@ApiModelProperty(value = "接收组")
	private String receciver;

	@ApiModelProperty(value = "触发类型")
	private Boolean timer;

	@ApiModelProperty(value = "cron表达式")
	private String cron;

	@ApiModelProperty(value = "屏蔽")
	private Boolean shield;

	@ApiModelProperty(value = "cron表达式")
	private String shieldTime;

	@ApiModelProperty(value = "模板变量")
	private String variables;

	@ApiModelProperty(value = "个性配置变量")
	private String extendInfo;

	@JsonIgnore
	private SendMessageJobStatus status;

	public SendMessageJob(SendMessageJobDto sendMessageJobDto) {
		if (Objects.nonNull(sendMessageJobDto)) {
			setId(sendMessageJobDto.getId());
			setJobName(sendMessageJobDto.getJobName());
			setTemplateId(sendMessageJobDto.getTemplateId());
			setRececiverType(sendMessageJobDto.getRececiverType());
			setRececiver(sendMessageJobDto.getRececiver());
			setTimer(sendMessageJobDto.getTimer());
			setCron(sendMessageJobDto.getCron());
			setShield(sendMessageJobDto.getShield());
			setShieldTime(sendMessageJobDto.getShieldTime());
			setVariables(Try.of(() -> new String(Base64.decode(sendMessageJobDto.getVariables().getBytes())))
					.getOrElse("{}"));
			setExtendInfo(Try.of(() -> JSON.toJSONString(sendMessageJobDto.getExtendInfo())).getOrElse("{}"));
		}

	}

	public SendMessageJob create() {
		setStatus(SendMessageJobStatus.CREATED);
		return this;
	}

	public SendMessageJob update() {
		return this;
	}

	public void createTask() {
		if (isCreated()) {
			setStatus(SendMessageJobStatus.TASK_CREATED);
		}
	}

	public void success() {
		if (isTaskCreated()) {
			setStatus(SendMessageJobStatus.SUCCESS);
		}
	}

	public void failure() {
		if (isCreated() || isTaskCreated()) {
			setStatus(SendMessageJobStatus.FAILURE);
		}
	}

	@JsonIgnore
	public boolean isTaskCreated() {
		return getStatus() == SendMessageJobStatus.TASK_CREATED;
	}

	@JsonIgnore
	public boolean isCreated() {
		return getStatus() == SendMessageJobStatus.CREATED;
	}

	@JsonIgnore
	public boolean isCancelled() {
		return getStatus() == SendMessageJobStatus.CANCELLED;
	}

	@JsonIgnore
	public boolean isGroup() {
		return this.getRececiverType() == RececiverType.USERID || this.getRececiverType() == RececiverType.WECHAT
				|| this.getRececiverType() == RececiverType.NULL;
	}

}
